package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lx
 * @Time: 14:41
 * @Description:
 * <bean class="类" id="id" >  </bean>  application.xml   Spring 年代   第一： application.xml   第二：注解    第三：SpringBoot
 * @Autoward
 * private 类型  id;  Java配置类
 *
 *   MQ 服务器中如果没有指定队列 才会重新创建  如果有了 不创建
 */
@Configuration
public class DeadLetterMqConfig {


    public static final String exchange_dead = "exchange.dead";//普通交换机 死信交换机
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";//TTL
    public static final String queue_dead_2 = "queue.dead.2";


    //队列 普通
    @Bean
    public Queue queue2(){
        return QueueBuilder.durable(queue_dead_2).build();
    }

    //队列 TTL
    @Bean
    public Queue queue1(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", exchange_dead);
        arguments.put("x-dead-letter-routing-key", routing_dead_2);
        //局部设置的TTL 优先级高于全局设置的TTL
        arguments.put("x-message-ttl", 10 * 1000);//全局设置  过期时间
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();

    }


    //交换机 普通交换机
    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }

    //
    @Bean
    public Binding binding1(){
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1).noargs();
    }

    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2).noargs();
    }

}
